// redis存取认证码配置
package util

import (
	"admin-api/common/constant"
	"admin-api/pkg/redis"
	"context"
	"log"
	"time"
)

var ctx = context.Background()

type RrdisStore struct{}

// 存认证码配置
func (r RrdisStore) Set(id string, value string) {
	key := constant.LOGIN_CODE + id
	err := redis.RedisDb.Set(ctx, key, value, time.Minute*5).Err()
	if err != nil {
		log.Panicln("redis set error:", err)
	}
}

// 取认证码配置
func (r RrdisStore) Get(id string, clear bool) string {
	key := constant.LOGIN_CODE + id
	val, err := redis.RedisDb.Get(ctx, key).Result()
	if err != nil {
		log.Panicln("redis get error:", err)
		return ""
	}
	if clear {
		redis.RedisDb.Del(ctx, key)
	}
	return val
}

// 认证认证码
func (r RrdisStore) Verify(id, answer string, clear bool) bool {
	v := r.Get(id, clear)
	return v == answer
}
